package cn.melonlib.cores.commons.comp.web.ctrl;

import cn.melonlib.cores.commons.comp.dao.FileSystem;
import cn.melonlib.cores.commons.comp.dao.FileSystemFileReader;
import cn.melonlib.cores.commons.comp.service.BasicService;
import cn.melonlib.cores.commons.comp.utils.EntityWrapperContainer;
import cn.melonlib.cores.commons.comp.web.anno.ControllerConfig;
import cn.melonlib.cores.commons.comp.web.utils.RequestUtils;
import cn.melonlib.cores.jpa.model.bean.collections.JsonMap;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.OkHttpClient;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.StreamUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

@ControllerConfig
public class BasicController extends ApplicationObjectSupport {

    public static final String APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8";

    public static final String TEXT_HTML_VALUE = "text/html;charset=UTF-8";

    @Resource
    protected ObjectMapper jsonMapper;

    @Resource
    protected BasicService basicService;

    @Resource
    protected EntityWrapperContainer entityWrapperContainer;

    @Resource
    protected HttpServletRequest request;

    @Resource
    protected RequestUtils requestUtils;

    @Resource
    protected OkHttpClient httpClient;

    @Resource
    protected FileSystem fileSystem;

    protected JsonMap<Object> createResponseByStatus(boolean status) {
        ControllerConfig controllerConfig = AnnotationUtils.findAnnotation(this.getClass(), ControllerConfig.class);
        return new JsonMap<>().append(controllerConfig.requestStatusName(), status);
    }

    protected JsonMap<Object> createResponseByCode(int code) {
        ControllerConfig controllerConfig = AnnotationUtils.findAnnotation(this.getClass(), ControllerConfig.class);
        return new JsonMap<Object>().append(controllerConfig.requestCodeName(), code);
    }

    public String get(String name) {
        return request.getParameter(name);
    }

    public <T> T getSessionAttr(String name) {
        return (T) request.getSession().getAttribute(name);
    }

    public <T> void setSessionAttr(String name, T val) {
        request.getSession().setAttribute(name, val);
    }

    public void downloadConfig(HttpServletRequest request, HttpServletResponse response, FileSystemFileReader reader) {
        System.out.println(request.getHeader("Range"));
        response.setHeader("Location", URLEncoder.encode(String.valueOf(reader.getFileName()), StandardCharsets.UTF_8));
        response.setHeader("Content-Disposition", "filename*=UTF-8''" + URLEncoder.encode(String.valueOf(reader.getFileName()), StandardCharsets.UTF_8));
        response.setContentLength(reader.getContentLength().intValue());
        response.setContentType(reader.getFileContentType());
    }
    public void downloadConfig(HttpServletRequest request, HttpServletResponse response, String fileName,Long contentLength,String contentType) {
        System.out.println(request.getHeader("Range"));
        response.setHeader("Location", URLEncoder.encode(String.valueOf(fileName), StandardCharsets.UTF_8));
        response.setHeader("Content-Disposition", "filename*=UTF-8''" + URLEncoder.encode(String.valueOf(fileName), StandardCharsets.UTF_8));
        response.setContentLength(contentLength.intValue());
        response.setContentType(contentType);
    }

    public void download(HttpServletResponse response, FileSystemFileReader reader) throws Exception {
        try (InputStream in = reader.getInputStream(); OutputStream out = response.getOutputStream()){
            StreamUtils.copy(in,out);
        }
    }

}
